<?php
Class Cal extends CI_Controller{
	function __construct(){
		parent::__construct();
		$this->load->model('cal_model');
	}
	
	function index(){
		$data['check'] = $this->check_event();
		$data['path'] = site_url("index.php/cal/get_schedule_json/");
		$this->load->view('cal_view.php',$data);
	}
	
	function get_schedule_json(){
		$calendar_id = $this->cal_model->get_calendar($this->session->userdata('user_id'));
		$events = $this->cal_model->get_events($calendar_id);
		if($events){
			foreach($events->result() as $cal){
				$cal_array[] = array(
					'id' => $cal->event_id,
					'title' => $cal->title,
					'start' => $cal->start,
					'end' => $cal->end,
					'url' => $cal->url,
					'allDay' => $this->is_allDay($cal->allDay),
					'color' => $cal->color,
					'description' =>$cal->detail,
					'place' => $cal->place,
					'type' => $cal->type
				);
			}
			echo json_encode($cal_array);
		}
		else echo "NONE";
		
	}
	
	function check_event(){
		$calendar_id = $this->cal_model->get_calendar($this->session->userdata('user_id'));
		$events = $this->cal_model->get_events($calendar_id);
		if($events){
			return 1;
		}
		else{
			return 0;
		}
	
	}
	
	function is_allDay($allDay){
		if($allDay=="true"){
			return true;
		}
		else return false;
	}
	function event_form(){
		$this->load->view('event_view');
	}
	
	function appointment_form(){
		$this->load->view('appointment_view');
	}
	
	function add_event(){
		$event = array(
			'calendar_id' => $this->cal_model->get_calendar($this->session->userdata('user_id')),
			'title' => $this->input->post('title'),
			'start' => $_POST['startdate'].'T'.$_POST['starttime'].':00',
			'end' => $_POST['enddate'].'T'.$_POST['endtime'].':00',
			'url' => '',
			'allDay' => 'false',
			'place' => $this->input->post('place'),
			'detail' => $this->input->post('detail'),
			'type' => 2,
			'color' => '#9400D3'
		);
		$this->cal_model->add_event($event);
		$this->index();
	}
	
	function add_appointment(){
		$event = array(
			'calendar_id' => $this->cal_model->get_calendar($this->session->userdata('user_id')),
			'title' => "Doctor Appointment",
			'start' => $_POST['startdate'].'T'.$_POST['starttime'].':00',
			'end' => $_POST['enddate'].'T'.$_POST['endtime'].':00',
			'url' => '',
			'allDay' => 'false',
			'place' => $this->input->post('place'),
			'type' => 1,
			'color' => '#FF4500',
		);
		$this->cal_model->add_event($event);
		$this->index();
	}
	
}